//////////////////////////////////////////////////////////////////////////
//
// Water class
// Water.h
// 
// Grant Johnson
// Qantm College
// 27/10/2010
//
//////////////////////////////////////////////////////////////////////////

#ifndef WATER_H
#define WATER_H

#include "Drawable.h"
#include <d3d9.h>
#include <d3d9types.h>

class Water : public Drawable
{
public:
	//Struct used to define the type of vertices
	struct VertexData
	{
		//Vertices have a position and a colour
		D3DVECTOR pos;
		D3DVECTOR normal;
		float u,v;
	};

	//Define Flexible Vertex Format as a static const that is capable of a 3d position and a colour
	static const DWORD VERTEX_FORMAT_FVF = D3DFVF_XYZ| D3DFVF_NORMAL | D3DFVF_TEX1;
	
	Water();
	virtual ~Water();

	//////////////////////////////////////////////////////////////////////////
	// VIRTUAL - Inherited from Drawable
	// For creating the drawable object
	//
	// Paramaters:
	// a_pDevice, the direct3d device
	// a_pTexFileName, a path passed into the function. This can either be used for
	// the texture file name, or for a mesh's file name.
	//
	// Returns false if any step of the creation failed
	// Returns true if created successfully
	//
	//////////////////////////////////////////////////////////////////////////
	virtual bool CreateObject(IDirect3DDevice9* a_pDevice, char* a_pTexFileName);

	//////////////////////////////////////////////////////////////////////////
	// VIRTUAL - Inherited from Drawable
	// For rendering the drawable object
	//
	// Paramaters:
	// a_pDevice, the direct3d device
	//
	// Returns void
	//
	//////////////////////////////////////////////////////////////////////////
	virtual void Render(float a_dt, IDirect3DDevice9* a_pDevice, const RenderParams& a_renderParams);

	void SetupVertices(D3DXVECTOR3* a_pVertsArray);
	void SetupIndices();

private:
		
	//////////////////////////////////////////////////////////////////////////
	// Calculates the lighting normals for each vertex in the terrain
	//
	// Paramaters:
	// None
	//
	// Returns void
	//
	//////////////////////////////////////////////////////////////////////////
	void CalculateNormals();
	
	//////////////////////////////////////////////////////////////////////////
	// Used to smooth out the terrain, calculates an average height based
	// on the vertices around it.
	//
	// Paramaters:
	// None
	//
	// Returns void
	//
	//////////////////////////////////////////////////////////////////////////
	void SmoothTerrain();

protected:
	//Store height map data
	float* m_pHeightData;

	//Number of vertices
	int m_gridVertX;
	int m_gridVertZ;

	//Cell spacing
	int m_terrainWidth;

	//Store the number of vertices, indices and primitives
	long m_vertCount;
	long m_indexCount;
	long m_primCount;

	//Store vertex and index data
	VertexData* m_pVertData;
	unsigned short* m_pIndexData;

	//Store the texture file name
	char* m_pTexFilename;

	//Direct3D texture pointer
	IDirect3DTexture9* m_pTex;

	//Material used on the terrain
	D3DMATERIAL9 m_mtl;
};

#endif //WATER_H